﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;

using AppToolKit.Core.Context.Spring;



using CenterDateAchieve.Pipeline;


using Model;
using Common.Logging;
using Model.TransfersConfig;
using Model.Hotel;
using BLL;
using BLL.Parser;

 
namespace CenterDateAchieve
{
    public partial class Form1 :Form
    {
        public Form1()
        {
            InitializeComponent();
            AutowiringComplete += new EventHandler(Form1_AutowiringComplete);
        }
       
        void Form1_AutowiringComplete(object sender, EventArgs e)
        {
            
        }
        private ContextAware context_;
        public event EventHandler AutowiringComplete;

        
        [AutoWiring]
        public ContextAware ContextAwareSlot
        {
            get { return context_; }
            set
            {
                context_ = value;
                if (AutowiringComplete != null)
                    AutowiringComplete(this, null);
            }
        }
        public const int MESSAGE_DEFAULT_SIZE = 200;
        private HotelBusiness business_;
        [AutoWiring]
        public HotelBusiness Business
        {
            get { return business_; }
            set { business_ = value; }
        }
       
        WorkThread work_;
        [AutoWiring]
        public WorkThread Workhread
        {
            get { return work_; }
            set { work_ = value; }
        }

        PipelineDataCollect conllect_;
        [AutoWiring]
        public PipelineDataCollect Collect
        {
            get { return conllect_; }
            set { conllect_ = value; }
        }
        TransfersTask task;
        TransfersParser parser = new TransfersParser();
     


        //IList<ShowMessage> list = null;
        private void button1_Click(object sender, EventArgs e)
        {
            work_.Stop();

            //logHelper.GetMainLogger().InfoFormat("停止线程");
            

            this.bt_Start.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ILog log = LogManager.GetLogger("App-Info");
            
            //log.InfoFormat("启动线程");
          //ILog logD=LogManager.GetLogger(typeof(Form1));
            if (task == null)
            {
               
                task = parser.GetTaskItem(@"TransfersTimeConfig.xml");

            }
            work_.Start(task);

            this.bt_Start.Enabled = false;
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            try
            {
                BindHotelList();



                task = parser.GetTaskItem(@"TransfersTimeConfig.xml");

                button2_Click(sender, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
           
           
        }
        /// <summary>
        /// 手动传输
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click_1(object sender, EventArgs e)
        {

            this.Cursor = Cursors.WaitCursor;

            this.button1.Enabled = false;
            List<string> hotels = GetHotel();
            //logHelper.GetMainLogger().InfoFormat("找到{0}个酒店", hotels.Count);
            foreach (string id in hotels)
            {
                foreach (Model.TransfersConfig.Hotelinfo h in task.Hotels)
                {

                    if (id == h.Hotelid)
                    {
                        foreach (TaskTable table in h.Tables)
                        {
                            try
                            {
                                DataSet ds3 = new DataSet();

                                PipelineTransferDescript descript = work_.ConvertToPipelineTransferDescript(table);

                                //wsDataInterface ws = new wsDataInterface(h.DateInterFace);
                                //ShowLog(table.Descript, h.Hotelid);
                                //logHelper.GetMainLogger().InfoFormat("开始获取数据{0},{1}", h.Hotelid, table.Descript);
                                //ds3 = ws.GetData(descript.DataSource);
                                //logHelper.GetMainLogger().InfoFormat("获取数据结束{0}", ds3.Tables[0].Rows.Count);
                                //ShowLog("获取数据结束", h.Hotelid);
                                //ds3.Tables[0].TableName = descript.SourceTable;
                                //List<PipelineTransferDescript> list = new List<PipelineTransferDescript>();
                                //list.Add(descript);
                                //ShowLog("开始更新数据", h.Hotelid);
                                //logHelper.GetMainLogger().InfoFormat("开始更新数据{0}", h.Hotelid);
                                //conllect_.Process_DataSet(list, ds3);
                                //logHelper.GetMainLogger().InfoFormat("更新数据结束{0}", h.Hotelid);
                                //ShowLog("更新数据结束", h.Hotelid);
                            }
                            catch 
                            {
                                //logHelper.GetErrorLogger().ErrorFormat("错误{0},{1}", h.Hotelid, ex.Message);
                                continue;
                            }

                        }
                    }
                }
            }
            //logHelper.GetMainLogger().InfoFormat("传输结束");
            this.button1.Enabled = true;
            this.Cursor = Cursors.Default;
        }

        private void BindHotelList()
        {
           IList<HotelInfo> list= business_.GetHotelInfo();

           Hotel_List.DataSource = list;
           Hotel_List.ValueMember = "Id";
           Hotel_List.DisplayMember = "Hotelname";
        
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(work_!=null)
            work_.Stop();
        }

        private List<string> GetHotel()
        {
            List<string> hotels = new List<string>();

           
            for (int i = 0; i < Hotel_List.Items.Count; i++)
            {
                if(Hotel_List.GetItemChecked(i))
                    hotels.Add(((HotelInfo)Hotel_List.Items[i]).Id);
            }
            return hotels;
        }
        private void ShowLog(string deal, string source)
        {
            if (lstMessage.Items.Count > MESSAGE_DEFAULT_SIZE)
            {
                lstMessage.Items.Clear();
            }

            ListViewItem item = lstMessage.Items.Add(source);
            item.SubItems.Add(deal);
            item.SubItems.Add(DateTime.Now.ToString());
        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

    }
   
}
